#include "descr/descr_regions_parser.h"
#include <sstream>
#include "region.h"


using namespace std;


bool DescrRegionsParser::parse(string line)
{
	bool isSuccessful = false;

	if (line[0] != '\t')
	{
		string regionId = popNextToken(line);
		shared_ptr<Region> region(new Region());
		region->id = regionId;
		parsingRegion = region;
		lineCount = 1;
		isSuccessful = true;
	}
	else
	{
		string token = popNextToken(line);
		switch (lineCount)
		{
			case 1: parsingRegion->settlementID       = token; break;
			case 2: parsingRegion->preferredFactionID = token; break;
			case 3: parsingRegion->rebelsID           = token; break;
			case 4:
			{
				unsigned int color = 0;
				for (int i = 0; i < 3; i++)
				{
					unsigned int channel;
					istringstream buffer(token);
					token = popNextToken(line);
					buffer >> channel;
					color = (color << 8) | ((unsigned char) channel);
				}
				parsingRegion->color = color;
				Region::add(parsingRegion);
			}
			break;
		}
		lineCount++;
		isSuccessful = true;
	}

	return isSuccessful;
}